/*
 * j4Win
 *  http://code.google.com/p/j4win/
 *
 * Copyright (C) 2006-2007 Felix Geyer, Moritz Laschinger
 *
 * This file is part of j4Win.
 *
 * j4Win is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * j4Win is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package j4win;

public class GameClient extends Game {
	private int lastCol = -1;
	
	public GameClient(GUI gui, PlayerType p1, PlayerType p2, KI[] ki, String ip, int port) {
		super(gui, p1, p2, ki);
		net = new NetworkClient(this, ip, port);
	}
	
	@Override
	protected void playerMove(int p, int col) {
		net.send(new NetPacket(NetPacket.ID.PlayerMove).add(col));
	}
	
	@Override
	public void start() {
		net.start();
		net.send(new NetPacket(NetPacket.ID.Version).add(Network.VERSION));
	}
	
	public void packetReceived(NetPacket packet) {
		switch (packet.getID()) {
			case NetPacket.ID.BadVersion:
				System.err.println("GameClient: Bad version");
				break;
			case NetPacket.ID.YourTurn:
				gui.turnChanged(1);
				requestMove(1, lastCol);
				break;
			case NetPacket.ID.OpponentTurn:
				gui.turnChanged(2);
				break;
			case NetPacket.ID.Field:
				field = Field.fromPacket(packet);
				field.swap();
				lastCol = packet.readInt();
				gui.playerMoved(field, lastCol);
				break;
			case NetPacket.ID.YouWon:
				gui.playerWon(1);
				endGame(true);
				break;
			case NetPacket.ID.OpponentWon:
				gui.playerWon(2);
				endGame(true);
				break;
			case NetPacket.ID.Tie:
				gui.tie();
				endGame(true);
			default:
				System.err.println("GameClient: Unknown packet received");
		}
	}
}
